//
//  XLTwoWayMatchView.m
//  XiangLove
//
//  Created by 陈宪栋 on 2018/3/7.
//  Copyright © 2018年 BAOMIHUA.COM. All rights reserved.
//

#import "XLTwoWayMatchView.h"
#import "XLMatchPopUpView.h"
#import "XLAccountManager.h"

@interface XLTwoWayMatchView ()
@property (nonatomic, strong) UIImageView *bgImageView;
@property (nonatomic, strong) UIImageView *circleImageView;
@property (nonatomic, strong) UIImageView *halfCircleImageView;
@property (nonatomic, strong) UIButton *matchButton;
@property (nonatomic, strong) XLMatchPopUpView *popUpView1;
@property (nonatomic, strong) XLMatchPopUpView *popUpView2;
@end

@implementation XLTwoWayMatchView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubViews];
    }
    return self;
}

- (void)addSubViews {
    _bgImageView = [[UIImageView alloc] init];
    _bgImageView.image = [UIImage imageNamed:@"home_bg_banner__n"];
    _bgImageView.contentMode = UIViewContentModeScaleAspectFill;
    [self addSubview:_bgImageView];
    [_bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    
    _circleImageView = [[UIImageView alloc] init];
    _circleImageView.image = [UIImage imageNamed:@"home_bg_quanquan_n"];
    [self addSubview:_circleImageView];
    [_circleImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self);
        make.size.mas_equalTo(CGSizeMake(188, 188));
    }];
    
    _halfCircleImageView = [[UIImageView alloc] init];
    _halfCircleImageView.image = [UIImage imageNamed:@"home_bg_quanquan_s"];
    _halfCircleImageView.hidden = YES;
    [self addSubview:_halfCircleImageView];
    [_halfCircleImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self);
        make.size.mas_equalTo(CGSizeMake(188, 188));
    }];
    
    _matchButton = [[UIButton alloc] init];
    [_matchButton setTitle:@"" forState:UIControlStateNormal];
    [_matchButton setTitle:@"" forState:UIControlStateSelected];
    [_matchButton setTitleColor:[UIColor clearColor] forState:UIControlStateSelected];
    [_matchButton setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
    [_matchButton setBackgroundImage:[UIImage imageNamed:@"home_but_pipei_n"] forState:UIControlStateNormal];
    [_matchButton setBackgroundImage:[UIImage imageNamed:@"home_but_quxiao_n"] forState:UIControlStateSelected];
    [self addSubview:_matchButton];
    [_matchButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self);
        make.size.mas_equalTo(CGSizeMake(72, 72));
    }];
    
    _popUpView1 = [[XLMatchPopUpView alloc] initWithFrame:CGRectMake(0, 0, 120, 50)];
    _popUpView1.textLabel.text = @"开始求伴";
    
    _popUpView1.hidden = NO;
    [self addSubview:_popUpView1];
    
    _popUpView2 = [[XLMatchPopUpView alloc] initWithFrame:CGRectMake(0, 0, 120, 50)];
    _popUpView2.textLabel.text = @"正在匹配中..";
    _popUpView2.hidden = YES;
    [self addSubview:_popUpView2];
}

- (void)beginAnimate {
    _halfCircleImageView.hidden = NO;
    _popUpView1.hidden = YES;
    _popUpView2.hidden = NO;
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    animation.duration = 2.5;
    animation.repeatCount = MAXFLOAT;
    animation.fromValue = @(0);
    animation.toValue = @(2 * M_PI);
    animation.removedOnCompletion = NO;
    [_halfCircleImageView.layer addAnimation:animation forKey:@"transform.rotation.z"];
}

- (void)stopAnimate {
    _halfCircleImageView.hidden = YES;
    _popUpView1.hidden = NO;
    _popUpView2.hidden = YES;
    [_halfCircleImageView.layer removeAllAnimations];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    _popUpView1.centerX = CGRectGetWidth(self.bounds) / 2;
    _popUpView1.top = CGRectGetHeight(self.bounds) / 2 + 58;
    _popUpView2.centerX = CGRectGetWidth(self.bounds) / 2;
    _popUpView2.top = CGRectGetHeight(self.bounds) / 2 + 58;
}

@end
